<?php
class Zendvn_Captcha_Image
{	
	public function createCaptcha($size = '')
	{		
		//Let's generate a totally random string using md5
		$md5_hash = md5(rand(0,999)); 
		//We don't need a 32 character long string so we trim it down to 5 
		$security_code = substr($md5_hash, 15, 6); 
	 
		//Set the session to store the security code
		$ns = new Zend_Session_Namespace('captcha');
		$ns->code = $security_code;
		
		//Set the image width and height
		if ($size != '' && $size == 'small')
		{
			$width = 120;
			$height = 40;
		}
		else {
			$width = 415;
			$height = 110;
		}
	 
		//Create the image resource 
		$image = imagecreatetruecolor($width, $height);  
		
		$color1 = imagecolorallocate($image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255));
		$color2 = imagecolorallocate($image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255));
		self::image_gradient($color1, $color2, $image, $width, $height);
	
		
			// Add a few random lines
			for ($i = 0, $count = mt_rand(5, 4 * 4); $i < $count; $i++)
			{
				$color = imagecolorallocatealpha($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(100, 255), mt_rand(50, 120));
				imageline($image, mt_rand(0, $width), 0, mt_rand(0, $width), $height, $color);
			}
	
			// Calculate character font-size and spacing
			$default_size = min($width, $height * 2) / (strlen($security_code) + 1);
			$spacing = (int) ($width * 0.9 / strlen($security_code));
	
			// Draw each Captcha character with varying attributes
			for ($i = 0, $strlen = strlen($security_code); $i < $strlen; $i++)
			{
				// Use different fonts if available
				//$font = mt_rand(1,5);
				$font = APPLICATION_PATH .'/fonts/font.ttf';
	
				// Allocate random color, size and rotation attributes to text
				$color = imagecolorallocate($image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
				$angle = mt_rand(-40, 20);
	
				// Scale the character size on image height
				$size = $default_size / 7 * mt_rand(8, 12);
				$box = imageftbbox($size, $angle, $font, $security_code[$i]);
	
				// Calculate character starting coordinates
				$x = $spacing / 4 + $i * $spacing;
				$y = $height / 2 + ($box[2] - $box[5]) / 4;
	
				// Write text character to image
				imagefttext($image, $size, $angle, $x, $y, $color, $font, $security_code[$i]);
			}
		
		header('Content-Type: image/png');
		imagepng($image);
		imagedestroy($image);
		exit();
	}
	
	private function image_gradient($color1, $color2, $image, $width, $height, $direction = NULL) 
	{
		$directions = array('horizontal', 'vertical');
	
		// Pick a random direction if needed
		if ( ! in_array($direction, $directions))
		{
			$direction = $directions[array_rand($directions)];
	
			// Switch colors
			if (mt_rand(0, 1) === 1)
			{
				$temp = $color1;
				$color1 = $color2;
				$color2 = $temp;
			}
		}
	
		// Extract RGB values
		$color1 = imagecolorsforindex($image, $color1);
		$color2 = imagecolorsforindex($image, $color2);
	
		// Preparations for the gradient loop
		$steps = ($direction === 'horizontal') ? $width : $height;
	
		$r1 = ($color1['red'] - $color2['red']) / $steps;
		$g1 = ($color1['green'] - $color2['green']) / $steps;
		$b1 = ($color1['blue'] - $color2['blue']) / $steps;
	
		if ($direction === 'horizontal')
		{
			$x1 =& $i;
			$y1 = 0;
			$x2 =& $i;
			$y2 = $height;
		}
		else
		{
			$x1 = 0;
			$y1 =& $i;
			$x2 = $width;
			$y2 =& $i;
		}
	
		// Execute the gradient loop
		for ($i = 0; $i <= $steps; $i++)
		{
			$r2 = $color1['red'] - floor($i * $r1);
			$g2 = $color1['green'] - floor($i * $g1);
			$b2 = $color1['blue'] - floor($i * $b1);
			$color = imagecolorallocate($image, $r2, $g2, $b2);
	
			imageline($image, $x1, $y1, $x2, $y2, $color);
		}
	}
} 
